﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using libtcodWrapper;
using Console = libtcodWrapper.Console;

namespace HDHack
{
    class LootInterface
    {
        Console displayConsole;
        List<Item> iList = new List<Item>();
        Player p;
        public bool IsActive = false;
        Dungeon theDungeon;

        public LootInterface(Player ip, Console dCon, Dungeon d)
        {            
            p = ip;
            displayConsole = dCon;
            theDungeon = d;
        }

        int ConvertItemID(int charVal)
        {
            int retVal = charVal - 97;
            return retVal;
        }

        public void DisplayLoot(List<Item> ItemList)
        {
            //displayConsole.PrintLine("What items do you want to pickup?", 1, 1, LineAlignment.Left); 
            displayConsole.Clear();
            iList = ItemList;
            int lastline = 1;
            int itemASCIIIndex = 97;
            if (iList.Count > 0)
            {
                lastline = displayConsole.PrintLineRect("The following items are here:", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                lastline++;
                displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                lastline++;


                foreach (Item i in iList)
                {
                    displayConsole.PrintLine("(" + Convert.ToChar(itemASCIIIndex) + ") " + i.name, 1, lastline, LineAlignment.Left);
                    itemASCIIIndex++;
                    lastline++;

                }
                displayConsole.PrintLineRect("Which item do you want?", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                IsActive = true;

            }
            else
            {
                displayConsole.PrintLine("No Items to Loot Here", 1, lastline, LineAlignment.Left);
                theDungeon.ComplexCommand = false;
                IsActive = false;
                
            }
        }

        public void ProcessChoice(KeyPress k)
        {
            int choiceIndex = ConvertItemID(k.Character);
            if (Convert.ToInt16(k.Character) > 96 && choiceIndex < iList.Count)
            {
                iList[choiceIndex].OnMap = false;
                p.PickUpItems(iList[choiceIndex]);
                iList.RemoveAt(choiceIndex);                
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
            else if (k.KeyCode == KeyCode.TCODK_ESCAPE)
            {
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
        }

    }
}
